public class chenbing{
    public static void main(String[] args) {
        int[] array = {-4, 1, 2, 2, 5, 5, 5, 8, 10};
        System.out.println(findIndex(array,array.length,6));
    }

    public static int findIndex(int[] array, int n, int target){
        int high = n - 1;
        //判断第一个值最大于要查找的值，则说明数组中没有小于target的值
        if (array[0] > target){
            return -1;
        }
        int low = 0;
        while (low <= high){
            int middle = low + (high-low)/2;
            //当中间值小于等于target值，则判断是不是最后一个，如果是最后一个，则说明是数组中最后一个小于等于目标的值
            //或者当后面的值大于target值，则说明此时的值为最后一个小于等于目标的值
            if (array[middle] <= target){
                if (middle == n-1 || array[middle+1]>target){
                    return middle;
                }
                low = middle+1;
            }else {
                high = middle -1;
            }
        }
        return -1;
    }
}